/* bank_reg_plot_final.do plots FXD position by treatment 
and combines it with position limit time seris.			
		
*  Input: 	PL_ts.dta				 
			reg_full_final.dta			 
			
*  Plots:   bank_pos_v3.eps 	// Figure 7
*/


clear all

grstyle init
grstyle set plain, horizontal
grstyle set color Set1


********************************************************************************
*	PART I. PL time series plot
********************************************************************************
 
/* Save PL time series plot with weighted average added */
use "./Data/PL_ts", clear 	// banka_data.do
label var mdate "Date"

keep if mdate>=tm(2008m1)
*g PL_avg= (PL_by0 + PL_by1) /2
label var PL_avg "Average"
label var PL_wavg "Weighted Averge"
label var PL_fb_ts_inv "Foreign banks"
label var PL_db_ts_inv "Domestic banks"

line PL*_inv PL_avg PL_wavg mdate, ytitle("Regulation") xtitle("Date") name(pl, replace) saving("./Temp/pl.gph", replace) ///
lwidth(medthick medthick thick thick) lpattern(solid shortdash solid solid) lcolor(gs0 gs0 navy maroon) yla(, format(%9.1f)) 



 

********************************************************************************
*	PART II. Plot FXD position by treatment (Figure 7)
********************************************************************************
use "./Data/reg_full_final", clear 

capture confirm variable Deriv_pos_abs
if _rc{
	g Deriv_pos_abs = abs(Deriv_pos) 
}

capture confirm variable Deriv_pos_abs_log
if _rc{
	g Deriv_pos_abs_log= log(Deriv_pos_abs*1e3+1)  // Deriv_pos is in 000.
	g Capital_log= log(CapitalUSDk*1e3+1)
}

capture confirm variable Capital_log 
if _rc{
	g Capital_log= log(CapitalUSDk*1e3+1)
}


save, replace 
 
/*Graphs */
sort mdate binding
by mdate binding: egen Agg_Deriv_pos_by= sum(Deriv_pos_abs)
by mdate binding: egen Avg_Deriv_pos_by= mean(Deriv_pos_abs)
by mdate binding: egen Deriv_pos_abs_log_by= mean(Deriv_pos_abs_log)

collapse *_by, by(binding mdate)
reshape wide *_by, i(mdate) j(binding)

g Avg_Deriv_pos_bio_by0= Avg_Deriv_pos_by0/1e6
g Avg_Deriv_pos_bio_by1= Avg_Deriv_pos_by1/1e6
g Agg_Deriv_pos_bio_by0= Agg_Deriv_pos_by0/1e6
g Agg_Deriv_pos_bio_by1= Agg_Deriv_pos_by1/1e6

g Agg_Deriv_pos_bio_total= Agg_Deriv_pos_bio_by0 + Agg_Deriv_pos_bio_by1
label var Agg_Deriv_pos_bio_total "Aggregate"

line Agg_Deriv_pos_bio_total mdate, xtitle("Date") ytitle("Aggregate Position ($ Billion)") lcolor(navy) ylabel(10 (20) 90) ///
	tline(2010m6) tline(2010m11, lpattern(dot)) /// 
	tline(2010m11, lpattern(dot)) ///
	tline(2011m5) tline(2011m8, lpattern(dot)) /// 
	tline(2012m11) tline(2013m2, lpattern(dot)) /// 
	name(check1, replace) saving("./Temp/aggpostot.gph", replace) ///
	tline(2016m6, lcolor(gray%50)) tline(2016m8, lpattern(dot) lcolor(gray%50))   ///
	legend(on) yscale(titlegap(*4))

g crit0= Avg_Deriv_pos_bio_by0 if mdate==tm(2010m6)
g crit1= Avg_Deriv_pos_bio_by1 if mdate==tm(2010m6)
egen Crit0= mean(crit0)
egen Crit1= mean(crit1)

g Avg_Deriv_pos_normdiv_by0=Avg_Deriv_pos_bio_by0/Crit0
g Avg_Deriv_pos_normdiv_by1=Avg_Deriv_pos_bio_by1/Crit1



elabel var (*by0) ("Unconstrained")
elabel var (*by1) ("Constrained")


sort mdate
line Avg_Deriv_pos_normdiv_by* mdate, ///
	xtitle("Date") ytitle("Average Position (Normalized)") ///
	name(check1, replace) saving("./Temp/avgposnormdiv.gph", replace) ///
	lpattern(shortdash solid) lcolor(navy navy) ///
	tline(2010m6) tline(2010m11, lpattern(dot)) /// 
	tline(2010m11, lpattern(dot)) ///
	tline(2011m5) tline(2011m8, lpattern(dot)) /// 
	tline(2012m11) tline(2013m2, lpattern(dot)) /// 
	tline(2016m6, lcolor(gray%50)) tline(2016m8, lpattern(dot) lcolor(gray%50)) ///

cd "$temp_path" 
gr combine aggpostot.gph avgposnormdiv.gph pl.gph, col(1) xcommon ysize(8)

cd "$FOLDER"
graph export "./Output/Figure_7_bank_pos3.eps", as(eps) preview(off) replace
graph export "./Output/Figure_7_bank_pos3.pdf", as(pdf) replace

 